using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using BesAsm.Emgaats.Core;
using BesAsm.Framework.ArcGisUtilities;
using BesAsm.Framework.Reporting;

namespace BesAsm.Emgaats.Workflow
{
  [ClassInterface(ClassInterfaceType.None)]
  public class WorkflowExtension : ESRI.ArcGIS.Desktop.AddIns.Extension, IEmgaatsExtension, IPersistVariant
  {

    private ArcGisUtils _arcUtils;
    private WorkflowUserControl _workflowUserControl;

    private Mediator _mediator;
    private LayerLibrarian _librarian;

    /// <summary>
    /// Raised when one of extension's data sources changes
    /// </summary>
    public event EventHandler RebindRequired;

    private static WorkflowExtension _workflowExtension;

    public WorkflowExtension()
    {
      _workflowExtension = this;      
    }

    internal static WorkflowExtension GetExtension()
    {
      if (_workflowExtension == null)
      {
        UID uid = new UIDClass();
        uid.Value = ThisAddIn.IDs.WorkflowExtension;
        ArcMap.Application.FindExtensionByCLSID(uid);
      }
      return _workflowExtension;
    }

    #region IPersistVariant Members

    public UID ID
    {
      get
      {
        UID typeID = new UIDClass();
        typeID.Value = GetType().GUID.ToString("B");
        return typeID;
      }
    }

    public void Load(IVariantStream Stream)
    {
      Marshal.ReleaseComObject(Stream);
    }

    public void Save(IVariantStream Stream)
    {
      Marshal.ReleaseComObject(Stream);
    }

    #endregion

    /// <summary>
    /// Returns an ArcUtils for performing common GIS tracer operations
    /// </summary>
    public ArcGisUtils ArcUtils
    {
      get { return this._arcUtils; }
    }

    /// <summary>
    /// Provides a means for this extension to perform custom startup
    /// </summary>
    /// <param name="initializationData"></param>
    protected override void OnStartup()
    {
      base.OnStartup();

      _mediator = Mediator.AttachToMediator(this);
      _librarian = LayerLibrarian.AttachToLibrarian(this);      

      BindLibrarian();

      _arcUtils = new ArcGisUtils(ArcMap.Application);
    }    
   
    /// <summary>
    /// Handles the ArcGIS NewDocument event
    /// </summary>
    protected void OnNewDocument()
    {

    }

    /// <summary>
    /// Handles the ArcGIS OpenDocument event
    /// </summary>
    protected void OnOpenDocument()
    {

    }

    /// <summary>
    /// Handles the LayerLibrarian CheckIn event
    /// </summary>
    /// <param name="sender">The object raising the CheckIn event</param>
    /// <param name="e">A CheckInCheckOutArgs object indicating which layer was checked in</param>
    protected void OnCheckIn(object sender, CheckInCheckOutArgs e)
    {
      //if (State == esriExtensionState.esriESEnabled)
        OnRebindRequired(EventArgs.Empty);
    }

    /// <summary>
    /// Handles the LayerLibrarian CheckOut event
    /// </summary>
    /// <param name="sender">The object raising the CheckOut event</param>
    /// <param name="e">A CheckInCheckOutArgs object indicating which layer was checked out</param>
    protected void OnCheckOut(object sender, CheckInCheckOutArgs e)
    {
      //if (State == esriExtensionState.esriESEnabled)
        OnRebindRequired(EventArgs.Empty);
    }

    /// <summary>
    /// Raised when one of the Tracer Extensions data sources (StartLinks,
    /// StopLinks, TraceLinks or MasterLinks) has been added or removed
    /// from the ArcGIS session.
    /// </summary>
    /// <param name="e">Default event arg, not used here</param>
    protected void OnRebindRequired(EventArgs e)
    {
      if (RebindRequired != null)
        RebindRequired(this, e);
    }

    /// <summary>
    /// Starts event-binding to the LayerLibrarian events
    /// </summary>
    private void BindLibrarian()
    {
      LayerLibrarian.CheckedIn += OnCheckIn;
      LayerLibrarian.CheckedOut += OnCheckOut;
      //if (State == esriExtensionState.esriESEnabled)
        OnRebindRequired(EventArgs.Empty);
    }

    /// <summary>
    /// Stops event-binding to the LayerLibrarian events
    /// </summary>
    private void UnbindLibrarian()
    {
      LayerLibrarian.CheckedIn -= OnCheckIn;
      LayerLibrarian.CheckedOut -= OnCheckOut;
    }

    /// <summary>
    /// Is extension compatible with system version?
    /// </summary>
    public bool IsExtensionCompatibleWithSystem
    {
      get
      {
        return Properties.Settings.Default.ExtensionSystemVersion ==
          Mediator.SystemVersion;
      }
    } 

    public string Caption
    {
      get
      {
        return "Emgaats System Diagnostics";
      }
    }

    public void ActivateExtension()
    {
      try
      {
        UID uid = new UIDClass();
        uid.Value = ThisAddIn.IDs.WorkflowUserControl;
        IDockableWindow win = ArcMap.DockableWindowManager.GetDockableWindow(uid);
        win.Show(true);
      }
      catch (Exception ex)
      {
        Messenger.ReportMessage("Error activating WorkflowExtension: " + ex.Message, ReportableMessageType.Error);
      }
       
    }

    public bool Enabled
    {
      get { return true; }// IsExtensionCompatibleWithSystem; }
    }
   
  }
}
